//
//  layerDelegate.m
/*
 Copyright 2009 Jack Whitsitt
 Conact: sintixerr@gmail.com | http://sintixerr.wordpress.com
 
 This file is part of "pkviz".
 
 "pkviz" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 "pkviz" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with "pkviz".  If not, see <http://www.gnu.org/licenses/>.
 */ 

#import "layerDelegate.h"


@implementation layerDelegate

//+(int) classCount;
//{
	//return classcount;
//}

//+(void) initialize {
   // classcount = 0;
//}

-(id)init
{
    if( (self = [super init]) ) 
	{  
		
	//	++classcount;
		
		int bytevalcap = 265;
		int byteposcap = 1600;
		
		byte_val = [NSMutableArray arrayWithCapacity:bytevalcap];
		byte_pos_for_val = [NSMutableArray arrayWithCapacity:byteposcap];

		
		int bv = 0;
		int bp = 0;
		
		while (bv < bytevalcap) 
		{
			while (bp < byteposcap) 
			{
				[byte_pos_for_val addObject:[NSNumber numberWithFloat:0.0]];
				
				++bp;
			}
			
			[byte_val addObject:byte_pos_for_val];
			++ bv;
		}
		
		[byte_val retain];
		
		
		avgpos = [NSMutableArray arrayWithCapacity:1600];
		poscount = [NSMutableArray arrayWithCapacity:1600];
				
		int ap = 0;
		
		while (ap < byteposcap) 
		{
	
			[avgpos addObject:[NSNumber numberWithFloat:0.0]];
			[poscount addObject:[NSNumber numberWithInt:0]];
			++ap;
		}
		
		[avgpos retain];
		[poscount retain];
		
		
    }
    return self;
}


- (void)readyNextPacket:(NSArray *)pk withIter:(int)iter andLayerCount:(int)maxLayers andHeadersExpanded:(int)headexp
{
	
	currentPacket = pk;
	pknum = iter;
	totalLayers = maxLayers;
	expandHeaders = headexp;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx // this is the layer's delegate method
{
	CGRect cgb = [layer bounds];
	
	[self drawInRect:cgb inContext:ctx]; // need different drawings, call different versions of this

	
}



- (void)drawInRect:(CGRect)b inContext:(CGContextRef)ctx // this draws
{
	
	CGMutablePathRef thePath = CGPathCreateMutable();

	NSEnumerator *lookingForGreen = [currentPacket objectEnumerator];
	NSArray *gByte;
	
	float mygreen = 1;
	
	while(gByte = [lookingForGreen nextObject]) // this should happen outside of the draw process...
	{

		
		if ([[gByte objectAtIndex:1] intValue] == 12)
		{
			mygreen = ([[gByte objectAtIndex:0] floatValue])/255;
		}
	}
	
		
	NSEnumerator *enumerator = [currentPacket objectEnumerator];
	NSArray *thisByte;


	
	while ((thisByte = [enumerator nextObject])) 
	{
		
		CGRect myDrawingRect;
		
		int bval = [[thisByte objectAtIndex:0] intValue];
		int bpos = [[thisByte objectAtIndex:1] intValue];
		
	
		byte_valpos_count = 0;
		
		byte_valpos_count = [[[byte_val objectAtIndex:bval] objectAtIndex:bpos] floatValue];
		byte_valpos_count = byte_valpos_count+1.0;
		
	
		[[byte_val objectAtIndex:bval] replaceObjectAtIndex:bpos withObject:[NSNumber numberWithFloat:byte_valpos_count]];
		
	
		int yeswehitit = [[poscount objectAtIndex:bpos] intValue];
		++yeswehitit;
		[poscount replaceObjectAtIndex:bpos withObject:[NSNumber numberWithInt:yeswehitit]];
		
	
		float newavg = ((bval + ([[avgpos objectAtIndex:bpos] floatValue] * (yeswehitit-1)))/yeswehitit);
		
		NSNumber *newavg2 = [NSNumber numberWithFloat:newavg];
		
		[avgpos replaceObjectAtIndex:bpos withObject:newavg2];
		
		
		float valavgRatio = bval/newavg;

		float myblue = 1;
		float myred = 0;
		
		if (valavgRatio > 1.1)
		{
			if (valavgRatio > 2)
			{
				valavgRatio = 2;
				myred = 1;
				myblue = .5;
			}
			else
			{
				myred = ((valavgRatio-1)/2)+.5;

			}

		}
		else if (valavgRatio < .9)
		{
			myred = .5-(valavgRatio/2);
			
		}

				
//		float x = (log10f(bpos)) * 472.292;
	
		float x;
		
		//NSLog(@"erm headersExpanded is: %i", [[self headersExpanded] intValue]);
		
		
		if ( expandHeaders == 1)
		{
			x = (CGRectGetWidth(b) * (log10f(bpos)))/3.176;
		}
		else
		{
			x = (CGRectGetWidth(b) * bpos)/1500;
		}

		float y = ((CGRectGetHeight(b) * bval)/255);

		
		float width = 1.1*(CGRectGetWidth(b)/1500);// + (.5*valavgRatio);
		float height =1.1*(CGRectGetHeight(b)/255);// + (.5*valavgRatio);
		

		
		myDrawingRect = CGRectMake(x, y, width, height);
		CGContextSetRGBFillColor(ctx, myred,mygreen,myblue,1); 
		
		CGContextFillRect(ctx, myDrawingRect); //hrm. this implies I cant have a path separate from a context, but that doesnt make sense - check it out.
			
			
		CGPathAddRect(thePath, nil, myDrawingRect); // add these paths to an array elsewhere right after read. then, when draw dont have to create them every time
		

	}
	//[thePath release];
	thePath = nil;
}



		
		
@end
